﻿using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Cat.Helper
{
    class Html_helper
    {
        public static string get_html(OpenReadCompletedEventArgs e)
        {
            return new StreamReader(e.Result).ReadToEnd();
        }
        /// <summary>
        /// 辅助下载的类
        /// </summary>
        /// <param name="download_path"></param>
        /// <param name="img_url"></param>
        public  bool DownLoad_helper(string download_path, string img_url)
        {
            new File_helper().File_existence(download_path);
            using (WebClient mywebclient = new WebClient())
            {
                try
                {
                    mywebclient.Headers.Add("Host", new Uri(img_url).Host);
                    mywebclient.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0");

                    string img_path = download_path + Path.GetFileName(img_url);
                    if (img_path.Length > 150)//文件名超长，会无法创建文件。所以改成当前时间得到的一个字符串
                        img_path = download_path + DateTime.Now.ToFileTime().ToString() + Path.GetExtension(img_url);
                  
                        mywebclient.DownloadFile(img_url, img_path);
                    return true;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    return false;
                }
            }
        }

        internal void DownLoad_helper(object download_path, string img_url)
        {
            throw new NotImplementedException();
        }


        /// <summary>
        /// 选择并返回HtmlNodeCollection的通用类
        /// </summary>
        /// <param name="htmldoc"></param>
        /// <param name="SelectNode"></param>
        /// <returns></returns>
        public HtmlNodeCollection Regex(String htmldoc, String SelectNode)
        {
            var htmlDoc = new HtmlAgilityPack.HtmlDocument();
            htmlDoc.LoadHtml(htmldoc);
            var htmlBody = htmlDoc.DocumentNode.SelectNodes(SelectNode);
            return htmlBody;
        }
        /// <summary>
        /// 获取超链接，返回list
        /// </summary>
        /// <param name="shtml">html</param>
        /// <param name="xpth">抓取规则</param>
        /// <returns></returns>
        public  List<String> Get_href(string shtml, string xpath) {
            List<String> href_list = new List<string>();
            try
            {
                foreach (HtmlNode line in new Helper.Html_helper().Regex(shtml, xpath))
                {

                    string url = line.Attributes["href"].Value;
                    href_list.Add(url);
                       
                }
            }
            catch(Exception e)
            {
                Console.WriteLine("链接抓取异常"+e);
            }
            return href_list;

        }

        public List<String> Get_src(string shtml, string xpath)
        {
            List<String> Src_list = new List<string>();
            try
            {
                foreach (HtmlNode line in new Helper.Html_helper().Regex(shtml, xpath))
                {

                    string url = line.Attributes["src"].Value;
                    Console.WriteLine(url);
                    Src_list.Add(url);

                }
            }
            catch (Exception e)
            {
                Console.WriteLine("链接抓取异常" + e);
            }
            return Src_list;

        }

        public List<String> Get_text(String shtml, string xpath) {

            List<String> Text_list = new List<string>();
            try
            {
                foreach (HtmlNode line in new Helper.Html_helper().Regex(shtml, xpath))
                {

                    string url = line.InnerText;
                 
                    Text_list.Add(url);

                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            return Text_list;

        }
        public List<String> Get_Html(String shtml, string xpath)
        {

            List<String> Html_list = new List<string>();
            try
            {
                foreach (HtmlNode line in new Helper.Html_helper().Regex(shtml, xpath))
                {

                
                    Html_list.Add(line.InnerHtml);

                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            return Html_list;

        }

        /// <summary>
        /// 需要一个htmldoc 和准备用来自定义的规则
        /// </summary>
        /// <param name="shtml"></param>
        /// <param name="custom_property"></param>
        /// <returns></returns>
        public String Remove_Unsafe(String shtml)
        {
            var htmlDoc = new HtmlAgilityPack.HtmlDocument();
            htmlDoc.LoadHtml(shtml);
         
            htmlDoc = Chlidern_delete(htmlDoc, "script");
            htmlDoc = Chlidern_delete(htmlDoc, "style");
            htmlDoc =  Chlidern_delete(htmlDoc, "//comment()");
         

            return htmlDoc.DocumentNode.InnerHtml;
        }

        private static HtmlDocument Chlidern_delete(HtmlDocument htmlDoc, string local_path)
        {
            try
            {


                foreach (HtmlNode comment in htmlDoc.DocumentNode.Descendants(local_path))
                {
                   
                    comment.Remove();
                }
            }
            catch (Exception ex){
                Console.WriteLine(ex);
            }
            return htmlDoc;
        }
    }
}
